package com.example.peibei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.SystemMessage;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.SystemMessagePresenter;
import com.example.peibei.ui.adapter.DynamicMessageAdapter;
import com.example.peibei.ui.adapter.SystemMessageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DynamicMessageActivity extends WDActivity {
    @BindView(R.id.rv_message)
    RecyclerView rv_message;
    @BindView(R.id.tv_title)
    TextView tv_title;

    private SPUtils spUtils;
    private String token;
    private List<SystemMessage.RecordsDTO> list = new ArrayList<>();
    private DynamicMessageAdapter systemMessageAdapter;
    private SystemMessagePresenter systemMessagePresenter;
    private int type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dynamic_message;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        type = bundle.getInt("type");
        if (type == 3) {
            tv_title.setText("广场消息");
        } else {
            tv_title.setText("交易评价");
        }
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        rv_message.setLayoutManager(new LinearLayoutManager(this));
        systemMessageAdapter = new DynamicMessageAdapter(list);
        rv_message.setAdapter(systemMessageAdapter);
        systemMessagePresenter = new SystemMessagePresenter(new MessageCall());
        systemMessageAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                SystemMessage.RecordsDTO data = (SystemMessage.RecordsDTO) adapter.getData().get(position);
                String subjectId = data.getSubjectId();
                if (type == 3) {
                    Bundle bundle = new Bundle();
                    bundle.putString("subjectId", subjectId);
                    bundle.putString("status", "0");
                    intent(DynamicDetailActivityTwo.class, bundle);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("publishOrderId", subjectId);
                    intent(OrderDetailActivity.class, bundle);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        systemMessagePresenter.reqeust(true, "Bearer " + token, type);
    }

    class MessageCall implements DataCall<SystemMessage> {

        @Override
        public void success(SystemMessage data, Object... args) {
            if (systemMessagePresenter.getPage() == 1) {
                list.clear();
            }
            systemMessageAdapter.setNewInstance(data.getRecords());
            systemMessageAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {
        }
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @Override
    protected void destoryData() {

    }
}
